package org.jeecg.modules.zcgl.job;

import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.properties.config.JeecgPropertyConfig;
import org.jeecg.common.util.DateUtils;
import org.jeecg.modules.system.entity.SysDepart;
import org.jeecg.modules.system.service.ISysDepartService;
import org.jeecg.modules.zcgl.service.IStatementAccountService;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;

/**
 * 对账单下载
 */
@Slf4j
public class StatementAccountJob implements Job {

    @Autowired
    private ISysDepartService departService;
    @Autowired
    private JeecgPropertyConfig propertyConfig;
    @Autowired
    private IStatementAccountService statementAccountService;

    @Override
    public void execute(JobExecutionContext jobExecutionContext) {
        long start = System.currentTimeMillis();
        log.info("对账单下载开始：" + start);
        try {

//            String dateStr = DateUtils.yyyyMMdd.get().format(DateUtils.nextDay(-1));
            Date date = DateUtils.nextDay(-1);
//        String dateStr = "20210122";

            List<SysDepart> departList = departService.queryCompanyList();
            if (departList == null) {
                return;
            }

            departList.stream().forEach(depart -> {
                try {
                    statementAccountService.statementAccountByDepart(depart, date);
                } catch (Exception e) {
                    log.error(depart.getDepartName() + "对账单下载失败：【" + e.getMessage() + "】");
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
            log.error("对账单下载失败！");
        }

        long end = System.currentTimeMillis();
        log.info("对账单下载单结束：" + end);
        log.info("对账单下载耗时：" + (end-start)/1000 + "S");

    }
}
